<?php
namespace app\Index\model;

use think\Db;
use think\Model;

class Consult extends Model
{
	//首页最新资讯轮播
	public function getHomeNew()
	{
		return Db::name('consult')->where(array('is_hot'=>1,'is_show'=>1,'nav_id'=>1,'is_vidoe'=>0))->order('id desc')->limit(5)->select();
	}

	//首页热门资讯
	public function getHotNew($where)
	{
		return Db::name('consult')->where($where)->where(array('is_show'=>1,'is_hot'=>1,'is_vidoe'=>0))->order('like desc')->limit(3)->select();
	}

	//首页热门资讯
	public function getHomeHotNew($where)
	{
		$data = Db::name('consult')->where($where)->where(array('is_show'=>1,'is_hot'=>1,'is_vidoe'=>0))->order('like desc')->limit(6)->select();
		foreach ($data as $key => $value) {
			$member = $this->getMemberInfo($value['user_id']);
			$data[$key]['member_name'] = $member['member_name'];
		}
		return $data;
	}

	//分类下的新闻
	public function getNew($where)
	{
		return Db::name('consult')->where($where)->where(array('is_show'=>1,'is_vidoe'=>0))->order('id desc')->limit(50)->select();
	}

	//标签
	public function getLabel($where='')
	{
		return Db::name('label')->where($where)->where(array('is_show'=>1))->order('sort desc')->select();
	}

	//单个标签
	public function getLabelOne($id)
	{
		return Db::name('label')->where(array('id'=>$id))->find();
	}

	//内容详情
	public function getContent($id)
	{
		Db::name('consult')->where(array('id'=>$id))->setInc('look');
		return Db::name('consult')->where(array('id'=>$id))->find();
	}

	//文章评论
	public function getCommentOne($id)
	{
		$comments = Db::name('comments')->where(array('cat_id'=>$id))->select();
		var_dump($comments);
	}

	//用户回复评论
	public function FunctionName($value='')
	{
		# code...
	}

	//收藏
	public function getCollect($id,$userId)
	{
		$gather = Db::name('users_gather')->where(array('user_id'=>$userId))->find();
		if ($gather['collect']) {
			$collect = explode(',',$gather['collect']);
			//记录文章是否收藏
			$is_cz = in_array($id,$collect);
			if ($is_cz) {
				$result = [
					'msg' => '你已收藏了',
					'result' => 2
				];
				return $result;
			}else{
				$data['collect'] = $gather['collect'].','.$id.',';
				$res = Db::name('users_gather')->where(array('user_id'=>$userId))->update($data);
				$result = [
					'msg' => '收藏成功',
					'result' => $res
				];
				return $result;
			}
		}else{
			$data['collect'] = ','.$id.',';
			$res = Db::name('users_gather')->where(array('user_id'=>$userId))->update($data);
			$result = [
				'msg' => '收藏成功',
				'result' => $res
			];
			return $result;
		}
	}

	public function getMemberInfo($id)
	{
		return Db::name('users')->where(array('user_id'=>$id))->find();
	}

	//搜索文章
	public function getSearchArticle($search)
	{
		if ($search == false) {
			$like = '%%';
		}else{
			$like = '%'.$search.'%';
		}
		$article = Db::table('tp_consult a')
		->field('a.*')
		->field('b.member_name,b.user_id,b.head_pic')
		->where('title|abstract','like',$like)
		->where(array('is_show'=>1,'is_vidoe'=>0,'status'=>2))
		->join('tp_users b','a.user_id = b.user_id')
		->order('id desc')
		->select();
		foreach ($article as $key => $value) {
			$label = explode(",",$value['label']);
        	$i=0;
	        foreach ($label as $k => $v) {
	            if ($v !='') {
	                $label_v[$i] = $this->getLabelOne($v);
	                $i++;
	            }
	        }
	        $article[$key]['label_v'] = $label_v;
		}
		return $article;
	}

	//搜索视频
	public function getSearchVideo($search)
	{
		if ($search == false) {
			$like = '%%';
		}else{
			$like = '%'.$search.'%';
		}
		$article = Db::table('tp_consult a')
		->field('a.*')
		->field('b.member_name,b.user_id,b.head_pic')
		->where('title|abstract','like',$like)
		->where(array('is_show'=>1,'is_vidoe'=>1,'status'=>2))
		->join('tp_users b','a.user_id = b.user_id')
		->order('id desc')
		->select();
		foreach ($article as $key => $value) {
			$label = explode(",",$value['label']);
        	$i=0;
	        foreach ($label as $k => $v) {
	            if ($v !='') {
	                $label_v[$i] = $this->getLabelOne($v);
	                $i++;
	            }
	        }
	        $article[$key]['label_v'] = $label_v;
		}
		return $article;
	}

	//搜索作者
	public function getSearchWriter($search)
	{
		if ($search == false) {
			$like = '%%';
		}else{
			$like = '%'.$search.'%';
		}
		$writer = Db::table('tp_users')
		->where('member_name','like',$like)
		->where(array('is_author'=>1))
		->select();
		foreach ($writer as $key => $value) {
			$writer[$key]['consult_list'] = Db::name('consult')->field('id,title')->where(array('user_id'=>$value['user_id'],'is_show'=>1))->limit(3)->order('id desc')->select();
		}
		return $writer;
	}

	//按照标签查找文章
	public function getLabelArticle($id)
	{
		if ($id == false) {
			$like = '%%';
			$article['label_name'] = '全部';
		}else{
			$like = '%,'.$id.',%';
			$label_name = $this->getLabelOne($id);
			$article['label_name'] = $label_name['name'];
		}
		$article = Db::name('consult')->where('label','like',$like)->where(array('is_show'=>1,'status'=>2))->select();
		foreach ($article as $key => $value) {
			$label = explode(",",$value['label']);
        	$i=0;
	        foreach ($label as $k => $v) {
	            if ($v !='') {
	                $label_v[$i] = $this->getLabelOne($v);
	                $i++;
	            }
	        }
	        $article[$key]['label_v'] = $label_v;
		}
		return $article;
	}

	//个人中心收藏
	public function getUserCollect($userId)
	{
		$collect = Db::name('users_gather')->field('collect')->where(array('user_id'=>$userId))->find();
		$i = 0;
		$collect = explode(',', $collect['collect']);
		foreach ($collect as $key => $value) {
			if ($value) {
				$con = Db::name('consult')->where(array('is_show'=>1,'status'=>2,'user_id'=>$value))->find();
				if (empty($con)) {
					continue;
				}
				$article[$i] = $con;
				$label = explode(",",$article[$i]['label']);
	        	$j=0;
		        foreach ($label as $k => $v) {
		            if ($v !='') {
		                $label_v[$j] = $this->getLabelOne($v);
		                $j++;
		            }
		        }
		        $article[$i]['label_v'] = $label_v;
				$i++;
			}
		}
		return $article;
	}

	//个人中心评论
	public function getUserComments($userId)
	{
		return Db::name('comments')->where(array('user_id'=>$userId,'is_show'=>1))->select();
	}

	public function getUserAttention($userId)
	{
		$attention = Db::name('users_gather')->field('attention')->where(array('user_id'=>$userId))->find();
		$i = 0;
		$attention = explode(',', $attention['attention']);
		$author = array();
		foreach ($attention as $key => $value) {
			if ($value) {
				$con = Db::name('users')
				->field('member_name,head_pic,user_id,signature,like,consult,browse,diss')->where(array('user_id'=>$value))->find();
				if (empty($con)) {
					continue;
				}
				$author[$i] = $con;
				$i++;
			}
		}
		return $author;
	}
}